package com.tools.cleanmaster.ui.activity;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.format.DateUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.tools.ad.ADConstant;
import com.tools.ad.EntranceType;
import com.tools.ad.adscene.inner.SplashInterstitialScene;
import com.tools.ad.util.GARecordUtils;
import com.tools.ad.util.StatusBarUtil;
import com.tools.cleanmaster.CleanConstant;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.ui.base.SingleActivity;
import com.tools.cleanmaster.ui.view.CommonWaveButton;

import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;


/**
 * CreatedXXX on 2017/6/5.
 */

public class SplashActivity extends SingleActivity implements View.OnClickListener {
    /**
     * 最长等待时长
     */
    public static final long MAX_ENTER_NEXT_STEP_TIME = 6 * DateUtils.SECOND_IN_MILLIS;

    private ViewGroup mClauseContainer;
    private CommonWaveButton mStartButton;
    private CheckBox mClauseCheck;

    /**
     * @param activity
     * @param
     */
    public static void setNavigationBar(Activity activity, int visible) {
        View decorView = activity.getWindow().getDecorView();
        //显示NavigationBar
        if (View.GONE == visible) {
            int option = SYSTEM_UI_FLAG_HIDE_NAVIGATION;
            decorView.setSystemUiVisibility(option);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        StatusBarUtil.setTransparent(this);
        setNavigationBar(this, View.GONE);
        init();

        io.display.sdk.Controller.getInstance().init(this, ADConstant.DISPLAY_APP_ID);
    }

    private void init() {
        initView();
        checkInitAd();
    }

    private void initView() {
        mClauseContainer = findViewById(R.id.clause_container);
        mStartButton = findViewById(R.id.start);

        mClauseCheck = findViewById(R.id.install_check);
        findViewById(R.id.layout_install_check).setOnClickListener(this);

        mStartButton.setText(getString(R.string.splash_start));
        mStartButton.addOnClickListener(this);

        TextView licenceAgreement = findViewById(R.id.tv_licence_agreement);
        TextView privacyPolicy = findViewById(R.id.tv_privacy_policy);

        licenceAgreement.setText(Html.fromHtml(getString(R.string.splash_installation_licence_agreement)));
        privacyPolicy.setText(Html.fromHtml(getString(R.string.splash_privacy_policy)));

        licenceAgreement.setOnClickListener(this);
        privacyPolicy.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_licence_agreement) {
            //使用协议
            Intent intent = new Intent();
            intent.putExtra(CleanConstant.CLAUSE_HTML_NAME, getString(R.string.term_of_use_html));
            intent.setClass(this, HelperActivity.class);
            startActivity(intent);

        } else if (v.getId() == R.id.tv_privacy_policy) {
            //隐私条款
            Intent intent = new Intent();
            intent.putExtra(CleanConstant.CLAUSE_HTML_NAME, getString(R.string.privacy_policy_html));
            intent.setClass(this, HelperActivity.class);
            startActivity(intent);

        } else if (v.getId() == R.id.start) {
            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK, CleanGAConstant.L_SPLASH_START_BTN);
            if (mClauseCheck.isChecked()) {
                imContext.getCleanSharedPrefManager().setAgreeClause();
                if (!((SplashInterstitialScene) imContext.getADModule().getAdSceneManager().getAdScene(EntranceType.SPLASH)).showAd()) {
                    sendEmptyMessage(R.id.msg_cleaner_go_next_step_finally);
                }
            } else {
                Toast.makeText(this, getString(R.string.splash_privacy_check_tip), Toast.LENGTH_SHORT).show();
            }
        } else if (v.getId() == R.id.layout_install_check) {
            mClauseCheck.setChecked(!mClauseCheck.isChecked());
        }

    }

    private void checkInitAd() {
        if (imContext.getADModule().isHmSDKInit()) {
            initAd();
        }

        //最长等待时间后，进入下一步( 直接打开主页  或  展示开始按钮)
        sendEmptyMessage(R.id.msg_cleaner_go_next_step_finally, MAX_ENTER_NEXT_STEP_TIME);
    }

    private void initAd() {
        try {
            if (imContext.getADModule().getAdSceneManager().load(EntranceType.SPLASH)) {
                imContext.getCleanRawCache().setHaveRequestedSplashAdvert(true);
            } else {
                goNextStep();
            }
        } catch (Exception e) {
            goNextStep();
        }
    }

    @Override
    public void subHandleMessage(Message msg) {
        super.subHandleMessage(msg);

        if (msg.what == R.id.msg_ad_sdk_init_finish) {
            initAd();
            msg.arg1 = R.id.msg_mob_message_end;

        } else if (msg.what == R.id.msg_cleaner_go_next_step_finally) {
            goNextStep();

        } else if (msg.what == R.id.msg_ad_interstitial_loaded) {
            removeEmptyMessage(R.id.msg_cleaner_go_next_step_finally);

            if (imContext.getCleanSharedPrefManager().isAgreeClause()) {
                imContext.getCleanRawCache().setFirstEnterApplication(false);
                ((SplashInterstitialScene) imContext.getADModule().getAdSceneManager().getAdScene(EntranceType.SPLASH)).showAd();
            } else {
                imContext.getCleanRawCache().setFirstEnterApplication(true);
                showSplashClause();
            }

            msg.arg1 = R.id.msg_mob_message_end;
        } else if (msg.what == R.id.msg_ad_interstitial_closed) {
            showMainActivity();
            msg.what = R.id.msg_mob_message_end;
        }
    }

    private void goNextStep() {
        removeEmptyMessage(R.id.msg_cleaner_go_next_step_finally);

        if (imContext.getCleanSharedPrefManager().isAgreeClause()) {
            imContext.getCleanRawCache().setFirstEnterApplication(false);
            showMainActivity();
        } else {
            imContext.getCleanRawCache().setFirstEnterApplication(true);
            showSplashClause();
        }
    }

    private void showMainActivity() {
        finish();
        customPendingTransition(R.anim.fade_in, R.anim.fade_out);

        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    private void showSplashClause() {
        mClauseContainer.setVisibility(View.VISIBLE);
        mStartButton.start();
    }

    @Override
    public void onBackStack() {
        //super.onBackStack();
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mStartButton.onDestroy();

        if (imContext.getCleanRawCache().isFirstEnterApplication()) {
            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_FINISH, CleanGAConstant.L_APPLICATION);
        }
    }
}
